package com.diamond.web.model.system;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.validator.constraints.NotBlank;

import com.diamond.core.util.ValidMessage;
import com.diamond.web.enums.MenuType;

@Entity
@Table(name="sys_permission")
@JsonIgnoreProperties({"parent"})
public class Permission implements Comparable<Permission>{
	
	@Id
	@GeneratedValue
    private Long id;

    @NotBlank(message=ValidMessage.PERMISSION_NAME_NULL)
    @Column(nullable=false)
    private String permissionName;

    @NotBlank(message=ValidMessage.PERMISSION_SIGN_NULL)
    @Column(nullable=false)
    private String permissionSign;

    @OrderBy
    private Integer sort;

    @ManyToOne
    @JoinColumn(name="parent_id")
    private Permission parent;
    
    private boolean isMenu;
    
    private String menuIcon;
    
    private MenuType menuType;


	public MenuType getMenuType() {
		return menuType;
	}

	public void setMenuType(MenuType menuType) {
		this.menuType = menuType;
	}

	public Permission() {
	}
    
    public Permission(Long id) {
    	this.id=id;
	}

	public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPermissionName() {
        return permissionName;
    }

    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName == null ? null : permissionName.trim();
    }

    public String getPermissionSign() {
        return permissionSign;
    }

    public void setPermissionSign(String permissionSign) {
        this.permissionSign = permissionSign == null ? null : permissionSign.trim();
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

	public boolean getIsMenu() {
		return isMenu;
	}

	public void setIsMenu(boolean isMenu) {
		this.isMenu = isMenu;
	}

	public String getMenuIcon() {
		return menuIcon;
	}

	public void setMenuIcon(String menuIcon) {
		this.menuIcon = menuIcon;
	}

	public Permission getParent() {
		return parent;
	}

	public void setParent(Permission parent) {
		this.parent = parent;
	}

	@Override
	public int compareTo(Permission o) {
		if(o!=null&&o.getSort()!=null&&this.sort!=null){
			return o.getSort().compareTo(this.sort);
		}
		return 0;
	}
	@Override
	public String toString() {
		return "Permisson:"+permissionName+" Sign:"+permissionSign+" MenuType:"+this.menuType;
	}
}